package com.jumper.property.comparer.core.convert;


import com.jumper.property.comparer.core.convert.impl.*;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 属性转换工厂
 */
public class PropertyConvertFactory {

    private static final Map<Class<?>, PropertyConverter> CONVERTER_MAP = new HashMap<>();

    static {
        CONVERTER_MAP.put(LocalDateTime.class, new LocalDateTimePropertyConverter());
        CONVERTER_MAP.put(LocalDate.class, new LocalDatePropertyConverter());
        CONVERTER_MAP.put(Date.class, new DatePropertyConverter());
        CONVERTER_MAP.put(BigDecimal.class, new BigDecimalPropertyConverter());
        CONVERTER_MAP.put(Boolean.class, new BooleanPropertyConverter());
        CONVERTER_MAP.put(boolean.class, new BooleanPropertyConverter());
    }

    /**
     * 注册一个转换器
     *
     * @param propertyClass 属性类型
     * @param converter     转换器实例
     */
    public static void register(Class<?> propertyClass, PropertyConverter converter) {
        CONVERTER_MAP.put(propertyClass, converter);
    }

    /**
     * 获取转换器
     */
    public static PropertyConverter getConverter(Class<?> propertyClass) {
        return CONVERTER_MAP.get(propertyClass);
    }
}
